package com.personalforumapi.repository;

import com.personalforumapi.entity.article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author:kinjaze
 * @Date:2020/9/23 15:54
 */
public interface articleRepostitory extends JpaRepository<article, Integer>{
    //通过文章的分类查询
    @Query(value = "select * from article where aclassification = :type",nativeQuery = true)
    List<article> findByType(@Param("type") String type);
    //最新文章
    @Query(value = "SELECT * FROM article ORDER  BY apubtime DESC",nativeQuery = true)
    List<article> findNew ();
    //最热文章
    @Query(value = "SELECT * FROM article ORDER  BY aview DESC",nativeQuery = true)
    List<article> findHot ();
    //更新页面的游览数据
    @Transactional//支持事务管理
    @Modifying(clearAutomatically = true)//表示执行增、删、改操作，clearAutomatically = true 表示清理实体类里的数据。
    @Query(value = "update article set aview = :aview where aid = :aid",nativeQuery = true)
    void updateView(@Param("aview") Integer aview,@Param("aid") Integer aid);
}
